package com.lishe.lishe.lishe.module.myorder.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.Button;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.myorder.fragment.OrdersFragment;
import com.lishe.lishe.lishe.utils.LogUtil;

/**
 * Created by lwa on 2017/8/11.
 * 我的订单viewpager容器
 */

public class MyOrdersActivity extends BaseActivity {

    private ViewPager viewPager;
    private Button btnBack;
    private TextView texTitle;
    private TabLayout tabLayout;
    private String[] titles;
    private int orderType = Globals.ORDER_TYPE_ALL;


    public static void jump(Activity activity, int orderType) {
        Intent intent = new Intent(activity, MyOrdersActivity.class);
        intent.putExtra("orderType", orderType);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titles = getResources().getStringArray(R.array.order_type);
        orderType = getIntent().getIntExtra("orderType", Globals.ORDER_TYPE_ALL);
        setContentView(R.layout.activity_my_orders);
        initView();
    }

    private void initView() {
        viewPager = findViewById(R.id.viewpager);
        tabLayout = findViewById(R.id.tabLayout);
        texTitle = findViewById(R.id.title_text_title);
        btnBack = findViewById(R.id.title_left_button);
        btnBack.setOnClickListener(v -> finish());
        texTitle.setText(R.string.my_order);
        initFragment();
    }

    /**
     * 初始化各个状态的订单
     */
    private void initFragment() {
        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return OrdersFragment.newInstance(position);
            }

            @Override
            public int getCount() {
                return titles.length;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setCurrentItem(orderType);
    }
}
